
var AjaxUrl = {
    "list": ajaxUrlBase + "tfzs/departments"
};
var tableElem = document.getElementById('tableEl');
function resBoxShow(flag){
    var $box = $("#resBox");
    flag ? $box.show() : $box.hide();
}
function resContentSwitch(state){
    var $con = $("#resContent");
    var $no  = $("#resNo");
    switch(parseInt(state)){
        case 1:
            $con.show();
            $no.hide();
            break;
        case 2:
            $con.hide();
            $no.show();
            break;
        default:
            $con.hide();
            $no.hide();
            break;
    }
}
$(document).ready(function(){
    // init
    PageInit();
    // submit
    $("#inSubmit").on("click", inSubmit);
    // out
    $("#outSubmit").on("click", outSubmit);
});
// 初始化
function PageInit(){
    inSubmit()
}
// submit
function inSubmit(){
    $("#resBox").show();
    // list
    loadListTable();
}
//输入参数
function getInData(params){
    var inData = {};

    if(typeof(params) != 'undefined'){
        inData.pageSize  = params.limit;
        inData.pageStart = params.offset;
        inData.sortName = params.sort;
        inData.sortOrder = params.order;
    }
    return inData;
}
var listData = [
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
    {id:0, sort:0, number: '0452161', name:"徐少全", sex:0, age:56, intime: "2018-09-08 13:07",  outtime: "2018-10-08 13:07", diagres:"定位：右侧颈内动脉系统 脑梗死TAOST：大脑动脉粥样硬化型"},
];

// 表格加载
function loadListTable(){
    //格式化文本
    function fatSex(value, row, index) {
        var content = (value == 0) ? "男" : "女";
        return [ content ];
    }
    //输入参数
    function inParams(params) {
        var inData = new getInData(params);
        return inData;
    }
    //table 返回的数据
    function resHandler(res){
        var dataObj = {"rows": [], "total": 0};
        var jsonData = eval(res)["data"];
        var dataRows  = jsonData['rows'];
        var dataTotal = jsonData['total'];
        if(dataTotal > 0) {
            var rowLen = dataRows.length;
            for (var i = 0; i < rowLen; i++) {
                var curObj = dataRows[i];
                dataRows[i] = curObj;
            }
            dataObj = { "rows": dataRows, "total": dataTotal };
            resContentSwitch(1);
        }else{
            resContentSwitch(2);
        }
        return dataObj;
    }
    // table
    BSTableFun({
        bstElem: tableElem,
        bstOpt: {
            method: "get",
            url: AjaxUrl["list"],
            uniqueId: "id",
            pageSize: 5,
            data: listData,
            sidePagination: 'client',
            queryParams: inParams,
            responseHandler: resHandler,
            columns: [
                { width:"5%", checkbox:true, align: 'center', valign: 'middle', halign: 'center'},
                { width:"5%",  field:"sort", title:"编号", align:'center', valign:'middle', halign:'center' },
                { width:"10%", field:"number", title:"住院号", align:'center', valign:'middle', halign:'center' },
                { width:"10%", field:"name", title:"患者姓名", align:'center', valign:'middle', halign:'center' },
                { width:"10%", field:"sex", title:"性别", align:'center', valign:'middle', halign:'center', formatter:fatSex },
                { width:"10%", field:"age", title:"年龄", align:'center', valign:'middle', halign:'center' },
                { width:"15%", field:"intime", title:"入院时间", align:'center', valign:'middle', halign:'center' },
                { width:"15%", field:"outtime", title:"出院时间", align:'center', valign:'middle', halign:'center' },
                { width:"20%", field:"diagres", title:"最终诊断", align:'left', valign:'middle', halign:'center' }
            ],
            onLoadSuccess: function(){

            }
        }
    });

    resContentSwitch(1);
}

/*************** 抽取 ****************/
var value = 0;
var progressTimer;
// 更新进度
function updateOutBarProgress(progress, current, all){
    document.getElementById('outBarValue').innerHTML = progress + "%";
    document.getElementById('outBarProgress').style.width = progress + "%";
}
function outSubmit(){
    layer.open({
        type: 1, anim: 2, title: false, shade: 0.5, shadeClose: false,
        skin: 'layui-layer-demo', area: ['560px', 'auto'],
        closeBtn: 0, btn: ['取消'], btnAlign: 'c',
        yes: function(){
            layer.msg("确定要取消抽取操作？");
        },
        content: $("#popDiv")
    });

    updateOutBarProgress(value);
    progressTimer = setInterval(function(){
        updateOutBarProgress(value);
        value++;
        if(value > 100){
            clearInterval(progressTimer);
        }
    }, 100);
}